package dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;

import bean.GroupBean;
import bean.UniversityBean;

import db.DbRow;
import db.DbSelect;
import db.DbTable;
import db.adapter.DbAdapterMySql;

class DbTableGroup extends DbTable {

	public DbTableGroup() throws InstantiationException,
			IllegalAccessException, ClassNotFoundException, SQLException {

	}


	@Override
	protected String _getName() {
		return "`group`";
	}

	@Override
	protected void _setAdapter() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
		con = new DbAdapterMySql().getConnection();
	}
}

public class GroupDAO extends UniversityDAO {
	private DbTableGroup gr;
	static private ArrayList ArrGroup;

	public GroupDAO() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
		gr = new DbTableGroup();
		if(ArrGroup == null){
			ArrGroup = new ArrayList();
		}
			
	}

	public ArrayList InitGroup() throws SQLException {
		DbSelect select = (DbSelect) gr.select().from(gr._getName()).where("1=1 ORDER BY id");
		ArrayList ar = (ArrayList) gr.fetchAll(select);

		for (int i = 0; i < ar.size(); i++) {
			DbRow row = (DbRow) ar.get(i);
			GroupBean group=new GroupBean((Integer)(row.getColomn("id")),(String)row.getColomn("name"),(String)row.getColomn("created"));
			ArrGroup.add(group);
		}
		return ArrGroup;
	}
	public int  Insert(UniversityBean object) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException{
		int idReturned;
		DbRow row=new DbRow(3);
		GroupBean gr=(GroupBean) object;
		row.setRow(gr.getId(), 0, "id");
		row.setRow((String )gr.getDenumire(), 1, "name");
		row.setRow((String )gr.getYearCreated(), 2, "Created");
		
		DbTableGroup tableGr=new DbTableGroup();
		
		idReturned=tableGr.insert(row);
		if(object.getId() == 0){
			gr.setId(idReturned);
		}
		
		ArrGroup.add(gr);
		
		return  idReturned;
		
	}
	
	public  void InsertUndoRendoo(UniversityBean object) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException{
		int idReturned;
		DbRow row=new DbRow(3);
		GroupBean gr=(GroupBean) object;
		
		row.setRow(gr.getId(), 0, "id");
		row.setRow((String )gr.getDenumire(), 1, "name");
		row.setRow((String )gr.getYearCreated(), 2, "Created");
		
		DbTableGroup tableGr=new DbTableGroup();
		
		tableGr.insert(row);
	}
	
	public void Delet(UniversityBean object) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException{
		
		GroupBean grBean=(GroupBean) object;
		DbTableGroup tableGr=new DbTableGroup();
		//remove from db
		tableGr.delete("id="+grBean.getId());
		//remove from arraylist 
		ArrGroup.remove(grBean);
		
	}

	@Override
	public void update(UniversityBean object) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
		
		GroupBean temp;
		
		GroupBean grBean=(GroupBean) object;
		DbTableGroup tableGr=new DbTableGroup();
		DbRow row=new DbRow(2);
		
		row.setRow((String )grBean.getDenumire(), 0, "name");
		row.setRow((String )grBean.getYearCreated(), 1, "Created");
		
		tableGr.update(row, "id="+grBean.getId());
		
		for(int i=0;i<ArrGroup.size();i++){
			temp=(GroupBean) ArrGroup.get(i);
			if(temp.getId()==grBean.getId()){
				ArrGroup.remove(i);
				ArrGroup.add(grBean);
			}
		}		
		
	}

	@Override
	public
	Vector getUniversityDAOById(int id) {
		Vector groupvector = new Vector();
		GroupBean temp;
		System.out.println("getUniversityDAOById "+ArrGroup.size());
		for(int i=0;i<ArrGroup.size();i++){
			temp=(GroupBean) ArrGroup.get(i);
			System.out.println(temp.getId()+" inca seva "+id);
			if(temp.getId()==id){
				System.out.println("am gasit");
				groupvector.add(temp.getDenumire());
				groupvector.add(temp.getYearCreated());
				return groupvector;
			}
		}	
		// TODO Auto-generated method stub
		return null;
	}
	
}
